package cn.leetcode;

import java.util.ArrayList;
import java.util.List;

public class Code46 {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.remove(0);
        System.out.println(list);
        Code46Demo code46Demo = new Code46Demo();
        List<List<Integer>> permute = code46Demo.permutePlus(new int[]{1, 2, 3});
        System.out.println(permute.toString());

    }

}


class Code46Demo {

    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> container = new ArrayList<>();
        fun(nums, result, container);
        return result;
    }

    public List<List<Integer>> permutePlus(int[] nums) {
        boolean[] booleans = new boolean[nums.length];
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> container = new ArrayList<>();
        fun(nums, result, container, booleans);
        return result;
    }

    public void fun(int[] nums, List<List<Integer>> result, List<Integer> container, boolean[] booleans){
        if(container.size() == nums.length){
            result.add(new ArrayList<>(container));
        }
        for (int i=0; i<nums.length; i++) {
            if(booleans[i]){
                continue;
            }
            container.add(nums[i]);
            booleans[i] = true;
            fun(nums, result, container, booleans);
            booleans[i] = false;
            container.remove(container.size() - 1);
        }
    }

    public void fun(int[] nums, List<List<Integer>> result, List<Integer> container){
        if(container.size() == nums.length){
            result.add(new ArrayList<>(container));
        }
        for (int num : nums) {
            boolean contains = container.contains(num);
            if(contains){
                continue;
            }
            container.add(num);
            fun(nums, result, container);
            container.remove(container.size() - 1);
        }
    }

    public void fun(boolean[] arr, List<List<Integer>> result, List<Integer> list, int[] nums){
        if(list.size() == nums.length){
            result.add(new ArrayList<>(list));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if(arr[i]){
                continue;
            }
            arr[i] = true;
            list.add(nums[i]);
            fun(arr, result, list, nums);
            list.remove(list.size()-1);
            arr[i] = false;
        }
    }


}